﻿using HslCommunication;
using HslCommunication.Profinet.Delta;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace CentralVacuum.Communication
{
    public static class HslDeltaSerial
    {
        //public static DeltaSerial deltaSerial = new DeltaSerial(1);//0=dvp系类;1=AS系类
        public static DeltaTcpNet deltaTcpNet = new DeltaTcpNet("10.10.150.1", 502, 1);//连接plc
        public static OperateResult connect;
        public static OperateResult<bool[]> readX0 = null;
        public static OperateResult<bool[]> readY0 = null;
        public static OperateResult<bool[]> readM0 = null;
        public static OperateResult<bool[]> readS0 = null;
        public static OperateResult<byte[]> readD2000 = null;
        public static bool[] gX0, gY0, gM0, gS0;
        #region "Private Section"
        #endregion
        //*************X区域**************
        #region "Private _x0"
        private static bool[] _x0 = new bool[250];
        #endregion

        #region "Public X0"
        public static bool[] X0
        {
            get
            {
                if (readX0 != null)
                {
                    if (readX0.IsSuccess)
                    {
                        for (int i = 0; i < readX0.Content.Length; i++)
                        {
                            _x0[i] = readX0.Content[i];
                        }
                    }
                }
                return _x0;
            }
        }
        #endregion

        //*************Y区域**************
        #region "Private _y0"
        private static bool[] _y0 = new bool[250];
        #endregion

        #region "Public Y0"
        public static bool[] Y0
        {
            get
            {
                if (readY0 != null)
                {
                    if (readY0.IsSuccess)
                    {
                        for (int i = 0; i < readY0.Content.Length; i++)
                        {
                            _y0[0] = readY0.Content[i];
                        }
                    }
                }
                return _y0;
            }
            set
            {
                if (value != _y0)
                {
                    for (int i = 0; i < readY0.Content.Length; i++)
                    {
                        _y0[i] = value[i];
                    }
                    //deltaSerial.Write("Y0", _y);
                    deltaTcpNet.Write("Y0", _y0);
                }
            }
        }
        #endregion

        //**************M区域***************
        #region "Private _m0"
        private static bool[] _m0 = new bool[250];
        #endregion
        #region "Public M0"

        public static bool[] M0
        {
            get
            {
                if (readM0 != null)
                {
                    if (readM0.IsSuccess)
                    {
                        for (int i = 0; i < readM0.Content.Length; i++)
                        {
                            _m0[0] = readM0.Content[i];
                        }
                    }
                }
                return _m0;
            }
            set
            {
                if (value != _m0)
                {
                    for (int i = 0; i < readM0.Content.Length; i++)
                    {
                        _m0[i] = value[i];
                    }
                    //deltaSerial.Write("M0", _m);
                    deltaTcpNet.Write("M0", _m0);
                }
            }
        }
        #endregion

        //**************S区域***************
        #region "Private _s0"
        private static bool[] _s0 = new bool[250];
        #endregion
        #region "Public S0"

        public static bool[] S0
        {
            get
            {
                if (readS0 != null)
                {
                    if (readS0.IsSuccess)
                    {
                        for (int i = 0; i < readS0.Content.Length; i++)
                        {
                            _s0[0] = readS0.Content[i];
                        }
                    }
                }
                return _s0;
            }
            set
            {
                if (value != _s0)
                {
                    for (int i = 0; i < readS0.Content.Length; i++)
                    {
                        _s0[i] = value[i];
                    }
                    //deltaSerial.Write("S0", _s);
                    deltaTcpNet.Write("S0", _s0);
                }
            }
        }
        #endregion
        //**************D区域***************
        #region "Private _d2000"
        private static string[] _d2000 = new string[100];
        #endregion
        #region "Public D2000"
        public static string[] D2000
        {
            get
            {
                if (readD2000 != null)
                {
                    if (readD2000.IsSuccess)
                    {
                        //_d2002 = deltaSerial.ByteTransform.TransSingle(readD.Content, 0).ToString();
                        _d2000[0] = deltaTcpNet.ByteTransform.TransSingle(readD2000.Content, 0).ToString();
                        _d2000[1] = deltaTcpNet.ByteTransform.TransSingle(readD2000.Content, 2).ToString();
                        _d2000[2] = deltaTcpNet.ByteTransform.TransSingle(readD2000.Content, 4).ToString();
                    }
                }
                return _d2000;
            }
            set
            {
                if (value != _d2000)
                {
                    _d2000 = value;
                    try
                    {
                        //deltaSerial.Write("D2002", Convert.ToSingle(_d2002));
                        if (connect.IsSuccess)
                        {
                            deltaTcpNet.Write("D2000", Convert.ToSingle(_d2000[0]));
                            deltaTcpNet.Write("D2002", Convert.ToSingle(_d2000[1]));
                            deltaTcpNet.Write("D2004", Convert.ToSingle(_d2000[2]));
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Message: {0}", e.Message);
                    }
                }
            }
        }
        #endregion

        public static void OpenConnect()
        {
            //bool最多批量读取256个数据,16位的short数据最多批量读取100个数据,32位的float,int数据最多批量读取50个数据!
            /*//deltaSerial = new DeltaSerial(1);//0=dvp系类;1=AS系类
            deltaSerial.SerialPortInni(sp =>
            {
                sp.PortName = "COM1";
                sp.BaudRate = 9600;
                sp.DataBits = 7;
                sp.StopBits = System.IO.Ports.StopBits.One;
                sp.Parity = System.IO.Ports.Parity.Even;
            });
            deltaSerial.Open(); //打开端口*/
            deltaTcpNet.Series = DeltaSeries.AS;//选择AS系类;
            connect = deltaTcpNet.ConnectServer();

            if (connect.IsSuccess)
            {
                //智能线程池读取PLC数据
                Task.Run(() =>
                {
                    ReadDataX();
                });
                Task.Run(() =>
                {
                    ReadDataY();
                });
                Task.Run(() =>
                {
                    ReadDataM();
                });
                Task.Run(() =>
                {
                    ReadDataS();
                });
                Task.Run(() =>
                {
                    ReadDataD();
                });
            }
            else
            {
                Console.WriteLine("connect failed:" + connect.Message);
                return;
            }
        }

        public static void ReadDataX()
        {
            while (true)
            {
                //readX = deltaSerial.ReadBool("X0", 256);
                readX0 = deltaTcpNet.ReadBool("X0", 250);
                Thread.Sleep(100);
                gX0 = X0;
            }
        }
        public static void ReadDataY()
        {
            while (true)
            {
                //readY = deltaSerial.ReadBool("Y0", 256);
                readY0 = deltaTcpNet.ReadBool("Y0", 250);
                Thread.Sleep(100);
                gY0 = Y0;
            }
        }
        public static void ReadDataM()
        {
            while (true)
            {
                //readM = deltaSerial.ReadBool("M0", 256);
                readM0 = deltaTcpNet.ReadBool("M0", 250);
                Thread.Sleep(100);
                gM0 = M0;
            }
        }
        public static void ReadDataS()
        {
            while (true)
            {
                //readS = deltaSerial.ReadBool("S0", 256);
                readS0 = deltaTcpNet.ReadBool("S0", 250);
                Thread.Sleep(100);
                gS0 = S0;
            }
        }

        public static void ReadDataD()
        {
            while (true)
            {
                //readD = deltaSerial.Read("D0", 100);
                readD2000 = deltaTcpNet.Read("D2000", 100);
                Thread.Sleep(100);
                //gD2000 = D2000;
            }
        }
    }
}
